<?php

class Multimedia_controller extends CI_Controller {
    
    function Multimedia_controller() {
        parent::__construct();
        
        $this->load->model('Multimedia_model');
    }
    
    public function index() {
        $data['images'] = $this->Multimedia_model->multimedia_list();
        $this->load->model('Menu_model');
        $data['options'] = $this->Menu_model->menu_root_list();
        
        $data['route'] = 'images';
        $this->admin($data);
    }
    
    public function admin($data) {
        $this->load->library('session');
        $data['lang'] = $this->session->userdata('lang');
        $data['member_id'] = $this->session->userdata('member_id');
        $data['firstname'] = $this->session->userdata('firstname');        
        $this->load->model('Panel_model');
        $data['panel'] = $this->Panel_model->admin_menus_list();
        foreach ($data['panel'] as $key => $option) {
            $data['panel'][$key]['option'] = $this->Panel_model->admin_options_list($option['admin_panel_id']);
        }
        
        $this->load->view('layout/admin', $data);
    }    
    
    public function delete($multimedia_id) {
        $this->Multimedia_model->multimedia_delete($multimedia_id);
        
        $this->index();
    }
    
    public function upload() {
        $this->do_upload();
    }
    
    function do_upload() {
        $config['upload_path'] = './public/files/images/';
        $config['allowed_types'] = 'gif|jpg|png';
        //$config['max_size']	= '100';
        //$config['max_width']  = '1024';
        //$config['max_height']  = '768';
        $config['encrypt_name'] = true;

        $this->load->library('upload', $config);
        
        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());

            $data['route'] = 'add_image';
            $data['error'] = $this->upload->display_errors();
            $this->load->view('layout/admin', $data);
        } else {
            $data = array('upload_data' => $this->upload->data());
            
            $this->set($this->input->post(), $this->upload->data());

            $this->load->view('layout/upload_success', $data);
            $this->index();
        }
    }
    
    public function set($post_data, $upload_data) {
        $this->Multimedia_model->multimedia_set(1, $post_data['name'], $upload_data['file_type'], $upload_data['file_name']);
    }
    
}

?>
